package com.sdgakj.rest;

import com.sdgakj.utils.PropUtil;
import com.sdgakj.utils.QnFileUploadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * 人员管理的Controller, :
 * 
 * @author liuwei
 */
@Controller
@RequestMapping(value = "/api/v1/upload")
public class AjaxRestController {

	private static final String DOMAIN_NAME_KEY = PropUtil.getInstance().get("qiniu_domain_name");

	private static Logger logger = LoggerFactory.getLogger(AjaxRestController.class);

	@RequestMapping(value = "/")
	@ResponseBody
	public String bulk(HttpServletRequest request, MultipartFile file) {
		System.out.println("upload start...");
		String url = "";
		if(file != null)
		{
			url = QnFileUploadUtil.uploadFile(file, file.getOriginalFilename());
			logger.info(url);
		}
		System.out.println(url);
		return url;

	}

	@RequestMapping(value = "base64fileUpload", method = {RequestMethod.POST})
	public void base64FileUpload(@RequestParam(value = "uploadfiles") MultipartFile file, HttpServletResponse response)
	{
		String url = "";
		if(file != null)
		{
			url = QnFileUploadUtil.uploadFile(file, file.getOriginalFilename());
			logger.info(url);
		}

		PrintWriter out = null;
		try
		{
			out = response.getWriter();
			out.println(url);
			out.flush();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "base64WatermarkFileUpload", method = { RequestMethod.POST })
	public void base64WatermarkFileUpload(@RequestParam(value = "uploadfiles") MultipartFile file,
										  HttpServletResponse response) {
		String url = "";
		if (file != null) {
			url = QnFileUploadUtil.uploadWatermarkFile(file, file.getOriginalFilename());
		}
		PrintWriter out = null;
		logger.info("URL==  "+url);
		try {
			out = response.getWriter();
			out.println(url);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
